package com.zmn.oms.task.job.third.daowei;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.common.constant.TodmConsts;
import com.zmn.oms.common.enums.ChannelFacade;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.third.core.tracker.FacadeTracker;
import com.zmn.oms.third.daowei.DaoweiConsts;
import com.zmn.oms.third.daowei.service.DaoweiService;
import com.zmn.oms.third.todm.Todm;
import com.zmn.oms.third.zhimi.ZhimiConsts;
import com.zmn.oms.third.zhimi.service.ZhimiOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 类描述：到位直选抢单任务
 *
 * @author LiangHailong
 * @date 2020/07/22 16:38
 */
@LTS
@Slf4j
public class DaoweiRushToAcceptOrderJob {
    @Autowired
    DaoweiService daoweiService;

    /**
     * 到位直选抢单
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_CHANNEL_RUSH_TO_ACCEPT_ORDER)
    public Result runWorkJob(Job job) {
        log.info("【到位直选】<抢单任务> 执行：" + job);

        String orderIdStr = job.getParam("orderId");
        if (!NumberUtil.isNumber(orderIdStr)) {
            Todm.tracker().success(false, "订单号错误").commit();
            return new Result(Action.EXECUTE_FAILED, "订单号错误!");
        }

        FacadeTracker.FacadeTrackerCore tracker = FacadeTracker.getTracker()
                .dataOut(ChannelFacade.DAOWEI)
                .remark("抢单")
                .orderId(orderIdStr);

        Long orderId = Long.valueOf(orderIdStr);
        try {
            daoweiService.rushToAcceptOrder(orderId);
            tracker.commit();
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        } catch (OmsBaseException e) {
            log.error(String.format("【到位直选】<抢单任务> ：%s", e.getMessage()), e);
            tracker.error(e, null).success(false).commit();
            return new Result(Action.EXECUTE_FAILED, e.getMessage());
        }
    }
}
